<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db; 
use think\Cookie; 
use think\Request;

class Specific extends Common
{

	/**
	 * [index 具体分类列表]
	 * @return [type] [description]
	 */
	public function index(){
		$request = Request::instance();
		$nav_id = $request->param('nav_id');
		$type_id = $request->param('type_id');
		// Db::table('specific')->where(['isFree'=>'yes','endTime'=>['<',time()]])->update(['isFree'=>'no']);
		if(input('post.') || input('get.') || $nav_id || $type_id){
			$datas = input('post.');
			if(empty($datas)){
				$datas = input('get.');
				if(empty($datas)){
					$datas['nav_id'] = $nav_id;
					$datas['type_id'] = $type_id;
					$datas['status'] = 'select';
				}
				$datas['status'] = 'select';
			}
			
			$arr = $this->select($datas);
			if(!empty($arr['status'])){
				if($arr['status'] == 'del'){
					$this->success('删除成功','index','',1);
				}else{
					$this->success('排序成功',url('index',['nav_id'=>$arr['nav_id'],'type_id'=>$arr['type_id']]),'',1);
				}
			}else if($arr['status'] === 0){
				$this->error('操作失败','index');
			}else{
				$data = $arr['arr'];
				$type_data = Db::table('type')->field('type_id,type_name')->where(['nav_id'=>$datas['nav_id']])->order('type_sort desc,type_id asc')->select();
				$nav_data = Db::table('nav')->field('nav_id,nav_name')->order('nav_sort desc')->select();
				$this->assign('nav_data',$nav_data);
				$this->assign('type_data',$type_data);
				$this->assign('data',$data);
				$this->assign('nav_id',$datas['nav_id']);
				$this->assign('type_id',$datas['type_id']);
				$this->assign('keywords',$datas['keywords']);
				return $this->fetch('index');
			}
		}else{
			$admin = unserialize(cookie('admin'));
			$where = array();
			$data = Db::table('specific')->field('s.*,t.type_name,n.nav_name,a.admin_name')->alias('s')->join('type t','t.type_id = s.type_id')->join('nav n','n.nav_id = s.nav_id')->join('admin a','a.admin_id = s.updated_admin_id')->order('nav_sort desc,s_sort desc,s_id asc')->where($where)->paginate(20);
			$type_data = Db::table('type')->field('type_id,type_name')->order('type_sort desc,type_id asc')->select();
			$nav_data = Db::table('nav')->field('nav_id,nav_name')->order('nav_sort desc')->select();
			$this->assign('nav_data',$nav_data);
			$this->assign('type_data',$type_data);
			$this->assign('data',$data);
			$this->assign('nav_id','');
			$this->assign('type_id','');
			return $this->fetch('index');		
		}
	}

	public function select($datas){
		if($datas['status'] == 'select'){
			$query = array();
			$where = array();
			if($datas['nav_id']){
				$query['nav_id'] = $datas['nav_id'];
				$where['s.nav_id'] = $datas['nav_id'];
			}
			if($datas['type_id']){
				$query['type_id'] = $datas['type_id'];
				$where['s.type_id'] = $datas['type_id'];
			}
			if($datas['keywords']){
				$query['keywords'] = $datas['keywords'];
				$keywords = $datas['keywords'];
				$where['s_name'] = ['like',"%$keywords%"];
			}
			if($datas['isFree']){
				$query['isFree'] = $datas['isFree'];
				$where['isFree'] = $datas['isFree'];
			}
			$pageParam['query'] = $query;
			$admin = unserialize(cookie('admin'));
			$arr = Db::table('specific')->field('s.*,t.type_name,n.nav_name,a.admin_name')->alias('s')->join('type t','t.type_id = s.type_id')->join('nav n','n.nav_id = s.nav_id')->join('admin a','a.admin_id = s.updated_admin_id')->where($where)->order('nav_sort desc,s_sort desc,s_id asc')->paginate(20,false, $pageParam);
			$data['status'] = '';
			$data['arr'] = $arr;
		}else if($datas['status'] == 'del'){
			$s_ids = $datas['s_id'];
			$s_id = implode(',', $s_ids);
			$del = Db::table('specific')->where('s_id','in',$s_id)->delete();
			if($del){
				$data['status'] = 'del';
			}else{
				$data['status'] = 0;
			}
		}else{
			$s_ids = $datas['s_id'];
			$s_sorts = $datas['s_sort'];
			if($s_ids){
				foreach ($s_ids as $key => $id) {
					$up = Db::table('specific')->where('s_id',$id)->update(['s_sort'=>$s_sorts[$id]]);
				}
				$data['status'] = 'sort';
				$data['nav_id'] = $datas['nav_id'];
				$data['type_id'] = $datas['type_id'];
			}
			
		}
		return $data;
	}


	/**
	 * [getType 获取到课程分类]
	 * @return [type] [description]
	 */
	public function getType(){
		$request = Request::instance();
		$nav_id = $request->param('nav_id');
		$type_data = Db::table('type')->field('type_id,type_name')->where(['nav_id'=>$nav_id])->order('type_sort desc,type_id asc')->select();
		echo json_encode($type_data);
	}


	/**
	 * [edit 具体分类修改页面]
	 * @return [type] [description]
	 */
	public function edit(){
		$request = Request::instance();
		$s_id = $request->param('s_id');
		$s_data = Db::table('specific')->where('s_id',$s_id)->find();
		if(input('post.')){
			$arr = input('post.');
			$arr['updated_time'] = date('Y-m-d H:i:s');
			$arr['updated_admin_id'] = $this->admin['admin_id'];
			if ($s_id){
				$variable = Db::table('specific')->where('s_id',$s_id)->update($arr);
			}else{
				$arr['created_time'] = $arr['updated_time'];
				$arr['created_admin_id'] = $arr['updated_admin_id'];
				$variable = Db::table('specific')->insert($arr);
			}
			if($variable){
				Db::table('course')->where('s_id',$s_id)->update(['type_id'=>$arr['type_id']]);
				$this->success('编辑成功',url('index',['nav_id'=>$arr['nav_id'],'type_id'=>$arr['type_id']]),'',1);
			}else{
				$this->error('编辑失败');
			}
		}else{
			$type_data = Db::table('type')->field('type_id,type_name')->where(['nav_id'=>$s_data['nav_id']])->order('type_sort desc')->select();
			$nav_data = Db::table('nav')->field('nav_id,nav_name')->order('nav_sort desc')->select();
			$this->assign('nav_data',$nav_data);
			$this->assign('type_data',$type_data);
			$this->assign('s_data',$s_data);
			return $this->fetch('edit');
		}
	}


	public function delete(){
		$request = Request::instance();
		$s_id = $request->param('s_id');
		if(Db::table('course')->where('s_id',$s_id)->find()){
			$this->error('抱歉！该分类下面有相关文章，不能被删除','index');
		}
		if(Db::table('specific')->where('s_id',$s_id)->delete()){
			$this->success('删除成功','index','',1);
		}else{
			$this->error('删除失败');
		}
	}
	
}